package cn.tedu.service;

import cn.tedu.dao.UserMapper;
import cn.tedu.pojo.User;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.List;

@Component
public class UserServiceImpl implements UserService{
    @Autowired
    private UserMapper userMapper;

    @Override
    public void userInsert(User user) {
        userMapper.insert(user);
    }

    @Override
    public User ourSelectById(Integer id) {
        return userMapper.selectById(id);
    }

    @Override
    public void ourDeleteByName(String name) {
        QueryWrapper<User> wrapper = new QueryWrapper();
        wrapper.eq("name", name);
        userMapper.delete(wrapper);
    }

    @Override
    public List<User> selectByNI(User user) {
        QueryWrapper<User> wrapper = new QueryWrapper(user);
        return userMapper.selectList(wrapper);
    }

    @Override
    public List<User> selectByAS(User user) {
        QueryWrapper<User> wrapper = new QueryWrapper();
        wrapper.lt("age", user.getAge())
                .eq("sex", user.getSex());
        return userMapper.selectList(wrapper);
    }

    @Override
    public List<User> selectLikeName(String s) {
        QueryWrapper<User> wrapper = new QueryWrapper();
        wrapper.like("name", s);
        return userMapper.selectList(wrapper);
    }

    @Override
    public List<User> selectOrderByAge(String sex) {
        QueryWrapper<User> wrapper = new QueryWrapper();
        wrapper.eq("sex", sex)
                .orderByDesc("age");
        return userMapper.selectList(wrapper);
    }

    @Override
    public List<User> selectOrderByAge2(Integer age) {
        QueryWrapper<User> wrapper = new QueryWrapper();
        wrapper.eq("age", age)
                .orderByDesc("id");
        return userMapper.selectList(wrapper);
    }

    @Override
    public List<User> selectSomething(User user) {
        boolean bid = user.getId()==null?false:true;
        boolean bname = user.getName()==null?false:true;
        boolean bage = user.getAge()==null?false:true;
        boolean bsex = user.getSex()==null?false:true;
        QueryWrapper<User> wrapper = new QueryWrapper();
        wrapper.eq(bid,"id", user.getId())
                .eq(bname,"name", user.getName())
                .eq(bage,"age", user.getAge())
                .eq(bsex,"sex", user.getSex());
        return userMapper.selectList(wrapper);
    }

    @Override
    public List<User> selectInId(Integer[] ids) {
        List<Integer> asList = Arrays.asList(ids);
        return userMapper.selectBatchIds(asList);
    }


}
